#pragma once
#include <string>
#include <jsoncpp/json/json.h>
#include "Log.hpp"

namespace RequestModule
{
    using namespace LogModule;

    class Request
    {
    public:
        Request() = default;
        Request(int x, int y, char oper) : _x(x), _y(y), _oper(oper)
        {
        }
        // 序列化
        bool Serialize(std::string &out_string)
        {
            Json::Value root;
            root["x"] = _x;
            root["y"] = _y;
            root["oper"] = _oper;
            out_string = root.toStyledString();
            return true;
        }
        // 反序列化
        bool Deserialize(std::string &in_string)
        {
            Json::Value root;
            Json::Reader reader;
            bool parsingSuccessful = reader.parse(in_string, root);
            if (!parsingSuccessful)
            {
                LOG(LogLevel::ERROR) << "Failed to parse JSON: " << reader.getFormattedErrorMessages();
                return false;
            }
            _x = root["x"].asInt();
            _y = root["y"].asInt();
            _oper = root["oper"].asInt();
            return true;
        }
        int X() const { return _x; }
        int Y() const { return _y; }
        int Oper()  const { return _oper; }
        ~Request()
        {
        }
    
    private:
        int _x;
        int _y;
        char _oper;
    };
}